﻿using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo.Extensions
{
    public static class CurveLoopExtension
    {
		/// <summary> 
		/// 曲线相交处切线相同
		/// </summary>
		/// <param name="loop"></param>
		/// <param name="npara"></param>
		/// <returns></returns>
		public static XYZ Evaluate(this CurveLoop loop, double npara)
		{
			XYZ point = null;
			IEnumerator<Curve> enumerator = loop.GetEnumerator();
			double endpara = 0;
			enumerator.Reset();
			while (enumerator.MoveNext())
			{
				Curve curve = enumerator.Current;
				double startpara = endpara;
				endpara += curve.Length / loop.GetExactLength();
				if (endpara >= npara && npara >= startpara)
				{
					npara = (npara - startpara) / (endpara - startpara);
					point = curve.Evaluate(npara, true);
					break;
				}
			}
			return point;
		}
		/// <summary>
		/// 曲线相交处切线相同
		/// </summary>
		/// <param name="loop"></param>
		/// <param name="npara"></param>
		/// <returns></returns>
		public static Transform ComputeDerivatives(this CurveLoop loop, double npara)
		{
			Transform transfrom = null;
			IEnumerator<Curve> enumerator = loop.GetEnumerator();
			double endpara = 0;
			enumerator.Reset();
			while (enumerator.MoveNext())
			{
				Curve curve = enumerator.Current;
				double startpara = endpara;
				endpara += curve.Length / loop.GetExactLength();
				if (endpara >= npara && npara >= startpara)
				{
					npara = (npara - startpara) / (endpara - startpara);
					transfrom = curve.ComputeDerivatives(npara, true);
					break;
				}
			}
			return transfrom;
		}

		public static bool IsIntersect(this CurveLoop loop, Line line, out XYZ point)
		{
			bool isIntersect = false;
			XYZ temp = null;
			foreach (var item in loop)
			{
				if (item.Intersect(line, out IntersectionResultArray resultArray) == SetComparisonResult.Overlap)
				{
					isIntersect = true;
					temp = resultArray.get_Item(0).XYZPoint;
					break;
				}
			}
			point = temp;
			return isIntersect;
		}


        /// <summary>
        /// 创建模型线
        /// </summary>
        /// <param name="document"></param>
        /// <param name="curve"></param>
        public static void CreateModelCurve(this Curve curve, Document document)
        {
            Plane plane;
            if (curve is Line)
            {
                Line line = curve as Line;
                if (line.Direction.IsAlmostEqualTo(XYZ.BasisZ))
                {
                    plane = Plane.CreateByThreePoints(curve.GetEndPoint(0), curve.GetEndPoint(1), curve.GetEndPoint(1) + XYZ.BasisX);
                }
                else
                {
                    plane = Plane.CreateByThreePoints(curve.GetEndPoint(0), curve.GetEndPoint(1), curve.GetEndPoint(1) + XYZ.BasisZ);
                }
            }
            else
            {
                plane = Plane.CreateByThreePoints(curve.GetEndPoint(0), curve.GetEndPoint(1), curve.Evaluate(0.5, true));
            }
            document.Create.NewModelCurve(curve, SketchPlane.Create(document, plane));
        }



    }
}
